<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php 
        $frist=!count($_POST);
        if($frist){
           //第一次访问 页面进行渲染
        ?>
           <h1>用户添加</h1>
        <form action="adduser.php" method="post">
            用户名：<input type="text" name="username"/><br/>
            密 &nbsp;&nbsp;码：<input type="password" name="upwd"/><br/>
            年 &nbsp;&nbsp;龄：
            <select name="age">
                <?php
                for($a=10;$a<100;$a++){
                 if($a==20){
                 echo "<option selected=\"selected\">${a}</option>"; 
                 }else{
                     echo "<option>${a}</option>"; 
                 }
                }
                ?>
                
            </select>
            <br/>
            性 &nbsp;&nbsp;别：
            <select name="sex">
                <option>男</option>
                <option selected="selected">女</option>
                <option>保密</option>
            </select>
            <br/>
            <input type="submit" value="确定"/>
            <input type="reset" value="取消" />
        </form>
        
        <?php
        }else{
          //不是第一次访问，进行数据处理
            $name=$_POST["username"];
            $pwd=$_POST["upwd"];
            $sex=$_POST["sex"];
            $age=$_POST["age"];
            $hostname="127.0.0.1";
            $username="root";
            $password="abc123";
            $database="mybb_test";
            $connect=mysqli_connect($hostname, $username, $password, $database);
            if($connect){
                  mysqli_set_charset($connect, "utf-8");
                $sqlstring="INSERT INTO `user_infos`( `username`, `upwd`, `age`, `sex`)"
                        . " VALUES ('${name}','${pwd}',${age},'${sex}')";
                           $result=mysqli_query($connect, $sqlstring);
                if($result){
                    echo "<script>alert('添加成功！');"
                        . "location='mysql001.php';</script>";
                }else{
                    echo "<script>alert('添加失败,请重试!!');"
                        . "location='adduser.php';</script>";
                }
            }
            mysqli_close($connect);
                    
        }
        ?>
     
        <?php
        
        ?>
    </body>
</html>
